package com.xfhy.selectcity.data;

import java.util.List;

import androidx.lifecycle.LiveData;
import androidx.room.Dao;
import androidx.room.Insert;
import androidx.room.OnConflictStrategy;
import androidx.room.Query;

/**
 * @author : xfhy
 * Create time : 2021/2/25 10:40
 * Description :
 */
@Dao
public interface AreaDao {

    //这里是LiveData的话  Room会在单独的线程上执行所有查询
    //LiveData当数据已更改时，observed将在主线程上通知观察者。
    @Query("SELECT * FROM city ORDER BY id ASC")
    LiveData<List<Area>> getAllAreas();

    /**
     * 根据省份名称查城市
     *
     * @param province 省份名
     */
    @Query("SELECT DISTINCT city FROM city WHERE province=:province ORDER BY id ASC")
    LiveData<List<String>> queryCitiesByProvince(String province);

    /**
     * 根据城市名称查区县
     *
     * @param city 城市名
     */
    @Query("SELECT * FROM city WHERE city=:city ORDER BY id ASC")
    LiveData<List<Area>> queryAreasByCity(String city);

    @Query("SELECT DISTINCT province FROM city")
    LiveData<List<String>> queryAllProvinceName();

    @Query("SELECT * FROM city WHERE province=:province ORDER BY id ASC")
    LiveData<List<Area>> queryAreasByProvince(String province);

    /**
     * 模糊查询 关键字
     */
    @Query("SELECT * FROM city WHERE province like '%' || :inputKey || '%' or city like '%' || :inputKey || '%' or country like '%' || :inputKey || '%' or " +
            "fullpinyin like '%' || :inputKey || '%' or shortpinyin like '%' || :inputKey || '%'" +
            " ORDER BY id " +
            "ASC")
    LiveData<List<Area>> searchAreasByKey(String inputKey);

}

